package top.healthylife.gateway.platform.eye.api.api.sifafengxian;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.utils.Get;
import top.healthylife.basics.utils.utils.SystemThreadLocal;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.core.util.SystemConfigUtils;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.dto.request.req.ApiReqDto;
import top.healthylife.gateway.feignapi.utils.ApiInvokeHelper;
import top.healthylife.gateway.platform.eye.api.config.SystemConfig;
import top.healthylife.gateway.platform.eye.api.util.OutHttpUtil;
import top.healthylife.gateway.platform.eye.dto.BaseRes.EyeResBaseResultDto;
import top.healthylife.gateway.platform.eye.dto.sifafengxian.EyeReqCourtRegisterDto.req.EyeReqCourtRegisterDto;
import top.healthylife.gateway.platform.eye.dto.sifafengxian.EyeReqCourtRegisterDto.res.EyeResCourtRegisterDto;
import top.heathylife.gateway.common.cons.SystemCons;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: GenUtil
 * @date: 2020-12-15 19:07:12
 */
@Slf4j
@Service
@ApiServiceExplain(price = 20, docUrl = "http://open.tianyancha.com/open/961", apiName = "立案信息[961]", info = "可以通过公司名称或ID获取立案信息，立案信息包括案件编号、案由、立案时间、案件原被告双方信息等字段的详细信息")
public class EyeCourtRegisterApi implements ParentFace<EyeReqCourtRegisterDto, EyeResCourtRegisterDto> {

    static final String URL = "http://open.api.tianyancha.com/services/open/jr/courtRegister/2.0";

    /**
     * 执行这个服务接口
     * 如果成功，返回成功结果，如果接口错误，直接抛异常即可
     *
     * @param eyeDto 请求参数
     * @return 查询的真实结果，如果是失败，则直接返回异常即可
     */
    @Override
    public EyeResCourtRegisterDto execute(EyeReqCourtRegisterDto eyeDto) {
        //可直接通过工具类获取这个对象
        SystemConfig o = (SystemConfig) SystemConfigUtils.get();
        return OutHttpUtil.get(URL, BeanUtil.beanToMap(eyeDto), o, EyeResCourtRegisterDto.class);
    }


    /**
     * 获取全量记录
     *
     * @param params 入参
     * @return
     */
    @SneakyThrows
    public <T extends EyeResBaseResultDto<M>,M> T getAllRecord(ApiReqDto params) {
        Map<String, Object> map = BeanUtil.beanToMap(params);
        String secret = SystemThreadLocal.get(SystemCons.secret);
        EyeReqCourtRegisterDto reqDto = new EyeReqCourtRegisterDto();
        reqDto.setPageSize(20);
        reqDto.setPageNum(1);
        reqDto.setKeyword(StrUtil.toString(map.get(Get.name(EyeReqCourtRegisterDto::getKeyword))));

        //首次调用获取总数,计算分页
        T t = ApiInvokeHelper.results(
                SystemCons.TYC_SHORT_NAME,
                this.getClass().getAnnotation(ApiServiceExplain.class).apiName(),
                reqDto,
                secret);

        //  结果集
        List<M> allRecord1 = new ArrayList<>(t.getItems());

        Integer total = t.getTotal();
        Integer pageSize = reqDto.getPageSize();
        int totalPages = (total + pageSize - 1) / pageSize;

        for (int i = 2; i <= totalPages; i++) {
            reqDto.setPageNum(i);
            t = ApiInvokeHelper.results(
                    SystemCons.TYC_SHORT_NAME,
                    this.getClass().getAnnotation(ApiServiceExplain.class).apiName(),
                    reqDto,
                    secret);
            allRecord1.addAll(t.getItems());
        }
        t.setItems(allRecord1);
        return t;
    }
}
